home *** CD-ROM | disk | FTP | other *** search
- Path: elroy.jpl.nasa.gov!alan
- From: alan@elroy.jpl.nasa.gov (Alan S. Mazer)
- Newsgroups: comp.lang.c
- Subject: Re: why is this an infinite loop?
- Date: 22 Jan 1996 18:46:46 GMT
- Organization: Image Analysis Systems Group, JPL
- Message-ID: <4e0m2m$cj7@elroy.jpl.nasa.gov>
- References: <4dp9p5$jm2@news1.wolfe.net> <tcpnntpd.16.1.20.14.37.24.2781597121.351172@the-fix.sos.on.ca>
- NNTP-Posting-Host: 137.78.80.141
-
- In article <tcpnntpd.16.1.20.14.37.24.2781597121.351172@the-fix.sos.on.ca>,
- <verneb@the-fix.sos.on.ca> wrote:
- >To: INT:neus@wolfenet.com
- >
- >Int:neus@wolfenet.com,
- >
- >I >Beginning c student can't figure out why this small function to read in a
- >I >value for the variable x causes an infinite loop when it receives
- >I >unexpected
- >I >input.
- >I >
- >I >double get_x()
- >I >{
- >I >int test;
- >I >double x;
- >I >
- >I >printf("Enter a numeric value for x: ");
- >I >
- >I >while(scanf("%lf", &x) != 1)/*could be here! change to ==1 */
- >I >{
- >I >printf("ERROR - program expecting a number");
- >I >printf("Enter a numeric value for x: ");
- >I >}
-
- If I understand the symptom correctly, it's because scanf doesn't remove bad
- input from the input stream. So if you type "a", for example, scanf will
- return 0 but leave the "a" in the input stream.
- --
-
- -- Alan # Mountain Dew and doughnuts...
- alan@elroy.jpl.nasa.gov # because breakfast is the most important meal
- # of the day.
-